#include "mapconfig.hpp"

MapConfig::MapConfig(QWidget *parent) : QDialog(parent) {
	_save = new QPushButton("Enregistrer");
	_quit = new QPushButton("Quitter");

	// Layout
	_generalLayout = new QVBoxLayout;
	_form_general = new QFormLayout;
	_form_background = new QFormLayout;
	_form_music = new QFormLayout;
	_layout_button = new QHBoxLayout;

	// GroupBox
	_general = new QGroupBox("Général");
	_background = new QGroupBox("Fond");
	_music = new QGroupBox("Musique");

	// Groupe General
	_map_author = new QLineEdit;
	_map_comment = new QTextEdit;
	_map_name = new QLineEdit;

	// Groupe Fond
	_change_background = new QPushButton("Parcourir");

	// Group Music
	_change_music = new QPushButton("Parcourir");

	// Button general
	_layout_button->addWidget(_save);
	_layout_button->addWidget(_quit);

	_generalLayout->addWidget(_general);
	_generalLayout->addWidget(_background);
	_generalLayout->addWidget(_music);
	_generalLayout->addLayout(_layout_button);

	// Create Fonction
	createGroupGeneral();
	createGroupFond();
	createGroupMusic();

	// Windows General
   setLayout(_generalLayout);
   setWindowTitle("Propriété de la carte");
   setFixedSize(400, 400);

   // Connect Signal et Slot
   connect(_quit, SIGNAL(clicked()), this, SLOT(quitter()));
   connect(_save, SIGNAL(clicked()), this, SLOT(save()));
   connect(_change_background, SIGNAL(clicked()), this, SLOT(changeBackground()));
   connect(_change_music, SIGNAL(clicked()), this, SLOT(changeMusic()));

}

// Methode Set
void MapConfig::setMap(Map *map) { _map = map; }

void MapConfig::createGroupGeneral() {
	_general->setLayout(_form_general);

	_form_general->addRow("Nom :", _map_name);
	_form_general->addRow("Auteur :", _map_author);
	_form_general->addRow("Commentaires :", _map_comment);

}

void MapConfig::createGroupFond() {
	_background->setLayout(_form_background);

	_form_background->addRow("Choisir fond :", _change_background);
	_change_background->setFixedWidth(100);
}

void MapConfig::createGroupMusic() {
	_music->setLayout(_form_music);

	_form_music->addRow("Choisir musique :", _change_music);
	_change_music->setFixedWidth(100);
}

void MapConfig::save() {
	if(_map_name->text() != "" && _map_name->text() != _map->getName()) {
		_map->setName(_map_name->text());
	_map->setChanged(true);
	}

	if(_map_author->text() != "" && _map_author->text() != _map->getAuthor()) {
		_map->setAuthor(_map_author->text());
		_map->setChanged(true);
	}

	if(_map_comment->toPlainText() != "" && _map_author->text() != _map->getComment()) {
		_map->setComment(_map_comment->toPlainText());
		_map->setChanged(true);
	}

	if(_PathBackground != "" && _PathBackground != _map->getPathBackground()) {
		_map->setPathBackground(_PathBackground);
		_map->getMapView()->changeBackground(_PathBackground);
		_map->setChanged(true);
	}

	if(_PathMusic != "" && _PathMusic != _map->getPathMusic()) {
		_map->setPathMusic(_PathMusic);
		_map->setChanged(true);
	}

	close();
}

void MapConfig::changeBackground() {
   _PathBackground = QFileDialog::getOpenFileName(this, "Changer le fond", QString(), "Images(*.png)");
}

void MapConfig::changeMusic() {
	_PathMusic = QFileDialog::getOpenFileName(this, "Changer la music");
}

void MapConfig::quitter() {
	_PathBackground = "";
	_PathMusic = "";

	close();
}

void MapConfig::clear() {
	_PathBackground = "";
	_PathMusic = "";
	_map_name->clear();
	_map_author->clear();
	_map_comment->clear();
}

void MapConfig::refresh() {
	_map_name->setText(_map->getName());
	_map_author->setText(_map->getAuthor());
	_map_comment->setText(_map->getComment());
}
